package com.example.zhangxun0527.MyShopDemo;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.example.zhangxun0527.MainActivity;
import com.example.zhangxun0527.R;

public class LoginActivity extends AppCompatActivity implements View.OnClickListener {

    private ImageView img_logo;
    private EditText et_uesrname;
    private EditText et_password;
    private CheckBox cb_remember;
    private TextView tv_forget;
    private TextView tv_regist;
    private Button btn_login;
    User user;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initView();
        btn_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (et_uesrname.getText().toString().isEmpty()||et_password.getText().toString().isEmpty()){
                    Toast.makeText(LoginActivity.this, "用户名密码不能为空", Toast.LENGTH_SHORT).show();
                }else {
                    if (cb_remember.isChecked()){
                        Intent intent =new Intent(LoginActivity.this, MainActivity.class);
                        intent.putExtra("user",user);
                        startActivity(intent);
                        finish();
                    }else {
                        Toast.makeText(LoginActivity.this, "请勾选记住密码", Toast.LENGTH_SHORT).show();
                    }
                }
            }
        });
    }


    private void initView() {
        img_logo = (ImageView) findViewById(R.id.img_logo);
        et_uesrname = (EditText) findViewById(R.id.et_uesrname);
        et_password = (EditText) findViewById(R.id.et_password);
        cb_remember = (CheckBox) findViewById(R.id.cb_remember);
        tv_forget = (TextView) findViewById(R.id.tv_forget);
        tv_regist = (TextView) findViewById(R.id.tv_regist);
        btn_login = (Button) findViewById(R.id.btn_login);

        btn_login.setOnClickListener(this);
        tv_regist.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_login:

                break;
            case R.id.tv_regist:
                Intent intent = new Intent(LoginActivity.this,RegistActivity.class);
                startActivityForResult(intent,100);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 100){
            user = (User) data.getSerializableExtra("myUser");
            String username = user.getUsername();
            String password = user.getPassword();

            et_uesrname.setText(username);
            et_password.setText(password);
        }
    }
}